package com.xiaoma.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xiaoma.entity.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117647047";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQClvHqn6rpe2bXJ1DbNC2Ced0pDtseREw+Au50fB2KI6CDubAb0XIcrZ0eZxaH5/EPCAAsab2hyU8cO6o8qB6/YF5ELMkKvGXmRsltDS8hKZToDrSyOJjYQcrLI+anBrdYeiA5BWdu3HW7wq6hq/swm3apGUdlVkLlE3vazXMdMQEdPywNVeD8s4UUolJNjcm/Z+JG7V8hOuaNc9dV/jrjEPfAkaGny9rctvOgt31TsSBDHAXoQf4sSZl4y1gZnUM090Q8tXPgXqcRfRNR0XbDR8iI0wtH1ot1EcJAdStvsvaG+vzkpn6zJj4hVPXmektmcOXDJdattHjj0qMvTxYmfAgMBAAECggEAJpsq7yZsV1/bSrJWOZkm3oLnF7Lh9dc8yz3osauktkNsOF2NmSpcii26rgUtX+/1UKF19Khy6f695amZw66atXl33eXNRsBmYxvqylhCxaGiy+xw7oPUICy3nL0bX/RAZkhHGc32skb26rTxvEqx/3OvEEZMm/PkVDijutAn1Ijb6nwjdIWbpdFW/L+YSlcMvwxkxzutrGzUYhXW6E8id29r1rOPp85vcCGVg4o9isAoPFP3btORI/u7CYgWBsONPQsfdRjq8rjaHmAialOxKLeApZmNH/g2z/oQ6GAXypEwyKf4cPUBznacV/arv9fJ2qMfM4krVUZ1nZ1Sr4jywQKBgQDoXVNNsIZGDs4eqHBY99QkUBLlob43RoRfcnL4yg6orrIzpGmddYwTLyaoOPKoLxYKXEpPZxERrohyQXwHOYPlz3Dx5kOUUOouRqvhWjPpaj4dVdiVFwtIW0rbcD0aKOl4hoA/6XLfVWIa71wZtFRUPAkRFbxWqhsPCFeqnaS30QKBgQC2mC5LaqMnk/ra3pwlDrGpgxpubcYoWz28rFOwtDEIbYT5RRP9O2k+oaJlkXQEsbJcVaZeKe9DTMcZKkf+ddkt2imSfctYPsC2aBz6TiVtcqAsw2BhzbMjJQhPpn83EmSn9u8IZQJ2zItsJbzSipD/WTJSYE6p8TtknUjGCgf2bwKBgGGORRo6AbXKI3m1qC2CJzvzrmtgg9b7MTpNd73CP3NMnR6XaYwlPOEVC7rW5fH8itlgtAYwNHdi+oBVw/fYiuriM/4rebX8XsV9AE7Fq0PVSizQzPSTTzZatcv5jH2+LX9foNq0bXkchOrgEZ/tGPagmgHlSURteJzQGNZWB0DxAoGBAKMRu96MiNNS3CjVwDt0KQSLWnKexObLvZIfkMpx0oyFSslXgOeaRH6z2ZW91hBPJkxCDcdwygafrfvyf8NoOiIyAQicIZtYx8DfsGl2mwRo51Wy0wPlsgNEEm0X8+saGRzMcmNb2lyAhk96UGzzZVU2srNQ4U/yaamMsi5zTPMvAoGBAMJ5LLm7CZN6h85ekpwr60MzVwDYVzfwHQf/wGA5wI6CT6njTt3QOrQucoYChSVBPdKGSxVEOLnGs4Vw4+8QpIJ5mQS9rPUXCVv3zMBgP+yLomkM++N7ftSJUBWUhiKmRMzVPc+d9DuldHemps+9TvJuLvp4J5+M6H3ICELSyM3x";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwqCwwn0pxuecWW7+hhC47hOFeVf7SGSSOXNb9BN26F02Y52VNMktRK77ORMj1Z5kAJVevDO0kx9LFvnc90p7K8atb307ol0kAzTtXNH87gOuQwdoU+nfURJhI5JrbylX/+OLpXDIzZn41tT/w+JsIbwSISIS33CKkUzCSIhI+M7G4+WQLOOfO0UOhVw5kiGniaYorFZetGpsMo7QT6Sdh1WaiXYodvrJWgoe42pC59WhBM3Ei2DqU0b18GA+hlT/NmXtKUR9JPg5aQ8Oc+n/tEwcPrtDxllbk2aPd5TE56R33B0BwFRHBQ5cxA4TkpJEXzYG56QvHtOE7lCsdhZ5mQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url ="http://xiaoma.free.idcfengye.com/api/order/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url= "http://baidu.com";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应："+result);

        return result;

    }
}
